package com.example.budget3.modify.dialogs;


import com.example.budget3.R;
import com.example.budget3.modify.RecurrTransaction;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.NumberPicker.OnValueChangeListener;
import android.widget.CheckBox;
import android.widget.NumberPicker;

public class DayOfMonthDialogFragment extends DialogFragment {
	
	private NumberPicker dayPicker;
	private CheckBox includeToday;
	private boolean today;
	private int day;
	private String tag;
	
	public static String getDayOfMonthSuffix(final int day) {
		
		String result = "";
		
		if (day >= 11 && day <= 13) {
			result += "th";
        }else{
	        switch (day % 10) {
	            case 1:  result += "st"; break;
	            case 2:  result += "nd"; break;
	            case 3:  result += "rd"; break;
	            default: result += "th"; break;
	        }
        }
		
		if(day >= 29){
			result += " or last day";
		}
		return result;
    }

	@Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
		
		if(savedInstanceState!=null){
			today = savedInstanceState.getBoolean("today");
			day = savedInstanceState.getInt("day");
		}else{  
			today = ((RecurrTransaction) getActivity()).getIncludeWeekToday();
			day = 0;
		}
		tag = getTag();
		LayoutInflater layoutInflater = getActivity().getLayoutInflater();
		View inflate = layoutInflater.inflate(R.layout.day_of_month_dialog, null);
		includeToday = (CheckBox) inflate.findViewById(R.id.checkBox1);
		includeToday.setText("Include This Month");
		includeToday.setChecked(today);
		if(tag == "recurrEdit"){
			includeToday.setChecked(false);
			includeToday.setVisibility(View.GONE);
		}
		
		
		int count = 31;
		dayPicker = (NumberPicker) inflate.findViewById(R.id.numberPicker);
    	
    	dayPicker.setWrapSelectorWheel(false);
    	
    	dayPicker.setMinValue(0);
    	dayPicker.setMaxValue(count - 1);
		String[] months = new String[count];
		for (int monthNum = 1; monthNum <= count; monthNum++) {
			months[monthNum-1] = monthNum + " " + getDayOfMonthSuffix(monthNum);
		}

		dayPicker.setDisplayedValues(months);
		dayPicker.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
		dayPicker.setOnValueChangedListener(new OnValueChangeListener() {
			@Override
			public void onValueChange(NumberPicker picker, int oldVal,
					int newVal) {
				day = newVal;
			}
		});
		dayPicker.setValue(day);
		AlertDialog.Builder adb = new AlertDialog.Builder( getActivity());
	    adb.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
            	today = includeToday.isChecked();
                ((RecurrTransaction) getActivity()).setDayOfMonth(day+1,today);
                dismiss();
            }
        }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {}
        });
		

	    AlertDialog dialogBox = adb.setTitle("Pick a Day Of Month")
	    		.setView(inflate).create();
	    dialogBox.show();
		return dialogBox;

    }
	
	
	@Override
	public void onSaveInstanceState(Bundle arg0) {
    	today = includeToday.isChecked();
		arg0.putBoolean("today", today);
		arg0.putInt("day", day);
		super.onSaveInstanceState(arg0);
	}

}
